package chapter3;

import java.util.Arrays;
import java.util.PriorityQueue;

public class MaxCover {

    public int maxCover(int[][] m){
        Line[] lines = new Line[m.length];
        for (int i = 0; i < m.length; i++) {
            lines[i] = new Line(m[i][0],m[i][1]);
        }
        Arrays.sort(lines,(Line o1,Line o2) -> {return o1.start - o2.start;});
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        int max = 0;
        for (int i = 0; i < m.length; i++) {
            while (!heap.isEmpty() && heap.peek() <= lines[i].start){
                heap.poll();
            }
            heap.add(lines[i].end);
            max = Math.max(max,heap.size());
        }
        return max;

    }

    public class Line{
        public int start;
        public int end;

        public Line(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public static void main(String[] args) {

    }
}
